package com.framework.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.framework.common.util.StringUtils;
import com.framework.workflow.mapper.ProcessInstanceMapper;
import com.framework.workflow.model.WfProcessInstance;
import com.framework.workflow.service.IProcessInstanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 流程业务关联服务
 */
@Slf4j
@Service
public class ProcessInstanceService implements IProcessInstanceService {
    @Autowired
    ProcessInstanceMapper mapper;

    @Override
    public WfProcessInstance findById(String id) {
        return mapper.selectById(id);
    }

    @Override
    public void save(WfProcessInstance instance) {
        if (StringUtils.isEmpty(instance.getId())) {
            mapper.insert(instance);
        } else {
            mapper.updateById(instance);
        }
    }

    @Override
    public WfProcessInstance findByProcInsId(String processInstanceId) {
        LambdaQueryWrapper<WfProcessInstance> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WfProcessInstance::getProcInstId, processInstanceId);
        return mapper.selectOne(queryWrapper);
    }

    @Override
    public WfProcessInstance findByBusinessId(String businessId) {
        LambdaQueryWrapper<WfProcessInstance> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WfProcessInstance::getBusinessId, businessId);
        return mapper.selectOne(queryWrapper);
    }
}
